from fastapi import FastAPI
from fastapi.responses import RedirectResponse
from langserve import add_routes
from langchain_core.runnables import RunnableLambda  # 导入 RunnableLambda

from open_ai import invoke_open_ai

app = FastAPI(
    title="LangChain服务器",
    version="1.0",
    description="使用LangChain的Runnable接口的简单API服务器",
)


@app.get("/")
async def redirect_root_to_docs():
    return RedirectResponse("/docs")


@app.get("/ping")
async def ping():
    return {"status": "ok", "message": "Service is running..."}


@app.get("/hello")
async def helloworld():
    result = invoke_open_ai("讲个小笑话")
    print(result)
    return result


if __name__ == "__main__":
    import uvicorn

    uvicorn.run(app, host="0.0.0.0", port=8000)
